
<!doctype html>
<html lang="zh" class="no-js">
  <head>
    
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width,initial-scale=1">
      
      
      
      
        <link rel="prev" href="../%E6%BF%80%E5%85%89%E5%8F%91%E5%B0%84%E5%99%A8/">
      
      
        <link rel="next" href="../%E8%A7%A6%E6%91%B8%E4%BC%A0%E6%84%9F%E5%99%A8/">
      
      
      <link rel="icon" href="../../assets/images/favicon.png">
      <meta name="generator" content="mkdocs-1.5.3, mkdocs-material-9.5.12">
    
    
      
        <title>蜂鸣器类 - CodeLabs(博客临时站点)</title>
      
    
    
      <link rel="stylesheet" href="../../assets/stylesheets/main.7e359304.min.css">
      
        
        <link rel="stylesheet" href="../../assets/stylesheets/palette.06af60db.min.css">
      
      


    
    
      
    
    
      
        
        
        <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
        <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,300i,400,400i,700,700i%7CRoboto+Mono:400,400i,700,700i&display=fallback">
        <style>:root{--md-text-font:"Roboto";--md-code-font:"Roboto Mono"}</style>
      
    
    
    <script>__md_scope=new URL("../..",location),__md_hash=e=>[...e].reduce((e,_)=>(e<<5)-e+_.charCodeAt(0),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script>
    
      

    
    
    
  </head>
  
  
    
    
      
    
    
    
    
    <body dir="ltr" data-md-color-scheme="default" data-md-color-primary="indigo" data-md-color-accent="indigo">
  
    
    <input class="md-toggle" data-md-toggle="drawer" type="checkbox" id="__drawer" autocomplete="off">
    <input class="md-toggle" data-md-toggle="search" type="checkbox" id="__search" autocomplete="off">
    <label class="md-overlay" for="__drawer"></label>
    <div data-md-component="skip">
      
        
        <a href="#_1" class="md-skip">
          跳转至
        </a>
      
    </div>
    <div data-md-component="announce">
      
    </div>
    
    
      

  

<header class="md-header md-header--shadow" data-md-component="header">
  <nav class="md-header__inner md-grid" aria-label="页眉">
    <a href="../.." title="CodeLabs(博客临时站点)" class="md-header__button md-logo" aria-label="CodeLabs(博客临时站点)" data-md-component="logo">
      
  <img src="../../OTHERS/dancehole.png" alt="logo">

    </a>
    <label class="md-header__button md-icon" for="__drawer">
      
      <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M3 6h18v2H3V6m0 5h18v2H3v-2m0 5h18v2H3v-2Z"/></svg>
    </label>
    <div class="md-header__title" data-md-component="header-title">
      <div class="md-header__ellipsis">
        <div class="md-header__topic">
          <span class="md-ellipsis">
            CodeLabs(博客临时站点)
          </span>
        </div>
        <div class="md-header__topic" data-md-component="header-topic">
          <span class="md-ellipsis">
            
              蜂鸣器类
            
          </span>
        </div>
      </div>
    </div>
    
      
        <form class="md-header__option" data-md-component="palette">
  
    
    
    
    <input class="md-option" data-md-color-media="" data-md-color-scheme="default" data-md-color-primary="indigo" data-md-color-accent="indigo"  aria-label="切换至夜间模式"  type="radio" name="__palette" id="__palette_0">
    
      <label class="md-header__button md-icon" title="切换至夜间模式" for="__palette_1" hidden>
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="m17.75 4.09-2.53 1.94.91 3.06-2.63-1.81-2.63 1.81.91-3.06-2.53-1.94L12.44 4l1.06-3 1.06 3 3.19.09m3.5 6.91-1.64 1.25.59 1.98-1.7-1.17-1.7 1.17.59-1.98L15.75 11l2.06-.05L18.5 9l.69 1.95 2.06.05m-2.28 4.95c.83-.08 1.72 1.1 1.19 1.85-.32.45-.66.87-1.08 1.27C15.17 23 8.84 23 4.94 19.07c-3.91-3.9-3.91-10.24 0-14.14.4-.4.82-.76 1.27-1.08.75-.53 1.93.36 1.85 1.19-.27 2.86.69 5.83 2.89 8.02a9.96 9.96 0 0 0 8.02 2.89m-1.64 2.02a12.08 12.08 0 0 1-7.8-3.47c-2.17-2.19-3.33-5-3.49-7.82-2.81 3.14-2.7 7.96.31 10.98 3.02 3.01 7.84 3.12 10.98.31Z"/></svg>
      </label>
    
  
    
    
    
    <input class="md-option" data-md-color-media="" data-md-color-scheme="slate" data-md-color-primary="black" data-md-color-accent="cyan"  aria-label="切换至日间模式"  type="radio" name="__palette" id="__palette_1">
    
      <label class="md-header__button md-icon" title="切换至日间模式" for="__palette_0" hidden>
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M12 7a5 5 0 0 1 5 5 5 5 0 0 1-5 5 5 5 0 0 1-5-5 5 5 0 0 1 5-5m0 2a3 3 0 0 0-3 3 3 3 0 0 0 3 3 3 3 0 0 0 3-3 3 3 0 0 0-3-3m0-7 2.39 3.42C13.65 5.15 12.84 5 12 5c-.84 0-1.65.15-2.39.42L12 2M3.34 7l4.16-.35A7.2 7.2 0 0 0 5.94 8.5c-.44.74-.69 1.5-.83 2.29L3.34 7m.02 10 1.76-3.77a7.131 7.131 0 0 0 2.38 4.14L3.36 17M20.65 7l-1.77 3.79a7.023 7.023 0 0 0-2.38-4.15l4.15.36m-.01 10-4.14.36c.59-.51 1.12-1.14 1.54-1.86.42-.73.69-1.5.83-2.29L20.64 17M12 22l-2.41-3.44c.74.27 1.55.44 2.41.44.82 0 1.63-.17 2.37-.44L12 22Z"/></svg>
      </label>
    
  
</form>
      
    
    
      <script>var media,input,key,value,palette=__md_get("__palette");if(palette&&palette.color){"(prefers-color-scheme)"===palette.color.media&&(media=matchMedia("(prefers-color-scheme: light)"),input=document.querySelector(media.matches?"[data-md-color-media='(prefers-color-scheme: light)']":"[data-md-color-media='(prefers-color-scheme: dark)']"),palette.color.media=input.getAttribute("data-md-color-media"),palette.color.scheme=input.getAttribute("data-md-color-scheme"),palette.color.primary=input.getAttribute("data-md-color-primary"),palette.color.accent=input.getAttribute("data-md-color-accent"));for([key,value]of Object.entries(palette.color))document.body.setAttribute("data-md-color-"+key,value)}</script>
    
    
    
    
      <div class="md-header__source">
        <a href="https://gitee.com/dancehole/code-labs" title="前往仓库" class="md-source" data-md-component="source">
  <div class="md-source__icon md-icon">
    
    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><!--! Font Awesome Free 6.5.1 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2023 Fonticons, Inc.--><path d="M439.55 236.05 244 40.45a28.87 28.87 0 0 0-40.81 0l-40.66 40.63 51.52 51.52c27.06-9.14 52.68 16.77 43.39 43.68l49.66 49.66c34.23-11.8 61.18 31 35.47 56.69-26.49 26.49-70.21-2.87-56-37.34L240.22 199v121.85c25.3 12.54 22.26 41.85 9.08 55a34.34 34.34 0 0 1-48.55 0c-17.57-17.6-11.07-46.91 11.25-56v-123c-20.8-8.51-24.6-30.74-18.64-45L142.57 101 8.45 235.14a28.86 28.86 0 0 0 0 40.81l195.61 195.6a28.86 28.86 0 0 0 40.8 0l194.69-194.69a28.86 28.86 0 0 0 0-40.81z"/></svg>
  </div>
  <div class="md-source__repository">
    dancehole/CodeLabs
  </div>
</a>
      </div>
    
  </nav>
  
</header>
    
    <div class="md-container" data-md-component="container">
      
      
        
          
        
      
      <main class="md-main" data-md-component="main">
        <div class="md-main__inner md-grid">
          
            
              
              <div class="md-sidebar md-sidebar--primary" data-md-component="sidebar" data-md-type="navigation" >
                <div class="md-sidebar__scrollwrap">
                  <div class="md-sidebar__inner">
                    



<nav class="md-nav md-nav--primary" aria-label="导航栏" data-md-level="0">
  <label class="md-nav__title" for="__drawer">
    <a href="../.." title="CodeLabs(博客临时站点)" class="md-nav__button md-logo" aria-label="CodeLabs(博客临时站点)" data-md-component="logo">
      
  <img src="../../OTHERS/dancehole.png" alt="logo">

    </a>
    CodeLabs(博客临时站点)
  </label>
  
    <div class="md-nav__source">
      <a href="https://gitee.com/dancehole/code-labs" title="前往仓库" class="md-source" data-md-component="source">
  <div class="md-source__icon md-icon">
    
    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><!--! Font Awesome Free 6.5.1 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2023 Fonticons, Inc.--><path d="M439.55 236.05 244 40.45a28.87 28.87 0 0 0-40.81 0l-40.66 40.63 51.52 51.52c27.06-9.14 52.68 16.77 43.39 43.68l49.66 49.66c34.23-11.8 61.18 31 35.47 56.69-26.49 26.49-70.21-2.87-56-37.34L240.22 199v121.85c25.3 12.54 22.26 41.85 9.08 55a34.34 34.34 0 0 1-48.55 0c-17.57-17.6-11.07-46.91 11.25-56v-123c-20.8-8.51-24.6-30.74-18.64-45L142.57 101 8.45 235.14a28.86 28.86 0 0 0 0 40.81l195.61 195.6a28.86 28.86 0 0 0 40.8 0l194.69-194.69a28.86 28.86 0 0 0 0-40.81z"/></svg>
  </div>
  <div class="md-source__repository">
    dancehole/CodeLabs
  </div>
</a>
    </div>
  
  <ul class="md-nav__list" data-md-scrollfix>
    
      
      
  
  
  
  
    <li class="md-nav__item">
      <a href="../.." class="md-nav__link">
        
  
  <span class="md-ellipsis">
    Home
  </span>
  

      </a>
    </li>
  

    
      
      
  
  
  
  
    <li class="md-nav__item">
      <a href="../../README_en/" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    README en
  </span>
  

      </a>
    </li>
  

    
      
      
  
  
  
  
    
    
    
    
    <li class="md-nav__item md-nav__item--nested">
      
        
        
        <input class="md-nav__toggle md-toggle " type="checkbox" id="__nav_3" >
        
          
          <label class="md-nav__link" for="__nav_3" id="__nav_3_label" tabindex="0">
            
  
  <span class="md-ellipsis">
    Git
  </span>
  

            <span class="md-nav__icon md-icon"></span>
          </label>
        
        <nav class="md-nav" data-md-level="1" aria-labelledby="__nav_3_label" aria-expanded="false">
          <label class="md-nav__title" for="__nav_3">
            <span class="md-nav__icon md-icon"></span>
            Git
          </label>
          <ul class="md-nav__list" data-md-scrollfix>
            
              
                
  
  
  
  
    <li class="md-nav__item">
      <a href="../../Git/github%2BpicGo%E5%9B%BE%E5%BA%8A/" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    github+picGo图床
  </span>
  

      </a>
    </li>
  

              
            
              
                
  
  
  
  
    <li class="md-nav__item">
      <a href="../../Git/git%E5%BE%BD%E6%A0%87/" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    GitHub项目徽标 - 知乎
  </span>
  

      </a>
    </li>
  

              
            
              
                
  
  
  
  
    <li class="md-nav__item">
      <a href="../../Git/%E4%B8%80%E6%96%87git%E6%95%99%E5%AD%A6/" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    GIT版本管理看这一篇就够了 - 简书
  </span>
  

      </a>
    </li>
  

              
            
              
                
  
  
  
  
    <li class="md-nav__item">
      <a href="../../Git/%E4%BC%98%E9%9B%85%E7%9A%84%E6%B7%BB%E5%8A%A0.gitignore/" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    VS Gitignore 示例：
  </span>
  

      </a>
    </li>
  

              
            
              
                
  
  
  
  
    <li class="md-nav__item">
      <a href="../../Git/%E5%85%B3%E4%BA%8Egit%E9%80%89license/" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    如何快速选择开源许可证License，看这三个就够了_copyright-CSDN博客
  </span>
  

      </a>
    </li>
  

              
            
              
                
  
  
  
  
    <li class="md-nav__item">
      <a href="../../Git/%E5%A6%82%E4%BD%95%E4%BC%98%E9%9B%85%E7%9A%84%E7%BB%99%E8%87%AA%E5%B7%B1%E7%9A%84%E4%BB%A3%E7%A0%81%E4%B8%8A%E4%BA%91/" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    如何优雅的给自己的代码上云
  </span>
  

      </a>
    </li>
  

              
            
              
                
  
  
  
  
    <li class="md-nav__item">
      <a href="../../Git/%E5%A6%82%E4%BD%95%E4%BC%98%E9%9B%85%E7%9A%84%E8%BF%9B%E8%A1%8C%E8%BF%9C%E7%A8%8B%E5%8D%8F%E4%BD%9C/" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    如何优雅的进行远程协作
  </span>
  

      </a>
    </li>
  

              
            
              
                
  
  
  
  
    <li class="md-nav__item">
      <a href="../../Git/%E8%B4%A1%E7%8C%AE%E6%96%87%E6%A1%A3/" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    贡献文档
  </span>
  

      </a>
    </li>
  

              
            
          </ul>
        </nav>
      
    </li>
  

    
      
      
  
  
  
  
    
    
    
    
    <li class="md-nav__item md-nav__item--nested">
      
        
        
        <input class="md-nav__toggle md-toggle " type="checkbox" id="__nav_4" >
        
          
          <label class="md-nav__link" for="__nav_4" id="__nav_4_label" tabindex="0">
            
  
  <span class="md-ellipsis">
    Linux UsefulCommands
  </span>
  

            <span class="md-nav__icon md-icon"></span>
          </label>
        
        <nav class="md-nav" data-md-level="1" aria-labelledby="__nav_4_label" aria-expanded="false">
          <label class="md-nav__title" for="__nav_4">
            <span class="md-nav__icon md-icon"></span>
            Linux UsefulCommands
          </label>
          <ul class="md-nav__list" data-md-scrollfix>
            
              
                
  
  
  
  
    <li class="md-nav__item">
      <a href="../../Linux-UsefulCommands/%E7%A8%8B%E5%BA%8F%E6%94%BE%E5%85%A5%E5%90%8E%E5%8F%B0%E8%BF%90%E8%A1%8C/" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    程序放入后台运行
  </span>
  

      </a>
    </li>
  

              
            
          </ul>
        </nav>
      
    </li>
  

    
      
      
  
  
  
  
    
    
    
    
    <li class="md-nav__item md-nav__item--nested">
      
        
        
        <input class="md-nav__toggle md-toggle " type="checkbox" id="__nav_5" >
        
          
          <label class="md-nav__link" for="__nav_5" id="__nav_5_label" tabindex="0">
            
  
  <span class="md-ellipsis">
    Markdown
  </span>
  

            <span class="md-nav__icon md-icon"></span>
          </label>
        
        <nav class="md-nav" data-md-level="1" aria-labelledby="__nav_5_label" aria-expanded="false">
          <label class="md-nav__title" for="__nav_5">
            <span class="md-nav__icon md-icon"></span>
            Markdown
          </label>
          <ul class="md-nav__list" data-md-scrollfix>
            
              
                
  
  
  
  
    <li class="md-nav__item">
      <a href="../../Markdown/Blog%E6%95%99%E7%A8%8B/" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    基于Mkdocs+mkdocs-material+gitcus+gitee/github page+..的个人博客页
  </span>
  

      </a>
    </li>
  

              
            
              
                
  
  
  
  
    <li class="md-nav__item">
      <a href="../../Markdown/%5B51cto%5DMarkdown%E7%94%A8%E6%B3%95/" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    [51cto]Markdown用法
  </span>
  

      </a>
    </li>
  

              
            
              
                
  
  
  
  
    <li class="md-nav__item">
      <a href="../../Markdown/%5Bcsdn%5Dmarkdown%E6%9C%80%E5%85%A8%E8%AF%AD%E6%B3%95/" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    [csdn]markdown最全语法
  </span>
  

      </a>
    </li>
  

              
            
              
                
  
  
  
  
    <li class="md-nav__item">
      <a href="../../Markdown/%5Bhtml%E6%89%A9%E5%B1%95%5Dmarkdown%E6%89%A9%E5%B1%95%E8%AF%AD%E6%B3%95/" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    [html扩展]markdown扩展语法
  </span>
  

      </a>
    </li>
  

              
            
              
                
  
  
  
  
    <li class="md-nav__item">
      <a href="../../Markdown/%5B%E6%B5%81%E7%A8%8B%E5%9B%BE%5D%E5%BC%80%E6%BA%90%E4%B8%89%E4%BB%B6%E5%A5%97-markdown%E6%89%A9%E5%B1%95/" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    [流程图]开源三件套 markdown扩展
  </span>
  

      </a>
    </li>
  

              
            
              
                
  
  
  
  
    <li class="md-nav__item">
      <a href="../../Markdown/%E5%BC%80%E6%BA%90%E4%B8%89%E4%BB%B6%E5%A5%97%E4%B9%8B%E4%B8%80-Markdown/" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    开源三件套之一 Markdown
  </span>
  

      </a>
    </li>
  

              
            
              
                
  
  
  
  
    <li class="md-nav__item">
      <a href="../../Markdown/%E7%BB%BC%E5%90%88%E5%BA%94%E7%94%A8%E5%AE%9E%E4%BE%8B/" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    应用实例：好看的主页
  </span>
  

      </a>
    </li>
  

              
            
              
                
  
  
  
  
    
    
    
    
    <li class="md-nav__item md-nav__item--nested">
      
        
        
        <input class="md-nav__toggle md-toggle " type="checkbox" id="__nav_5_8" >
        
          
          <label class="md-nav__link" for="__nav_5_8" id="__nav_5_8_label" tabindex="0">
            
  
  <span class="md-ellipsis">
    README模板
  </span>
  

            <span class="md-nav__icon md-icon"></span>
          </label>
        
        <nav class="md-nav" data-md-level="2" aria-labelledby="__nav_5_8_label" aria-expanded="false">
          <label class="md-nav__title" for="__nav_5_8">
            <span class="md-nav__icon md-icon"></span>
            README模板
          </label>
          <ul class="md-nav__list" data-md-scrollfix>
            
              
                
  
  
  
  
    <li class="md-nav__item">
      <a href="../../Markdown/README%E6%A8%A1%E6%9D%BF/readme_en/" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    Readme en
  </span>
  

      </a>
    </li>
  

              
            
              
                
  
  
  
  
    <li class="md-nav__item">
      <a href="../../Markdown/README%E6%A8%A1%E6%9D%BF/readme_zh/" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    Readme zh
  </span>
  

      </a>
    </li>
  

              
            
          </ul>
        </nav>
      
    </li>
  

              
            
          </ul>
        </nav>
      
    </li>
  

    
      
      
  
  
  
  
    
    
    
    
    <li class="md-nav__item md-nav__item--nested">
      
        
        
        <input class="md-nav__toggle md-toggle " type="checkbox" id="__nav_6" >
        
          
          <label class="md-nav__link" for="__nav_6" id="__nav_6_label" tabindex="0">
            
  
  <span class="md-ellipsis">
    PublishedArticle
  </span>
  

            <span class="md-nav__icon md-icon"></span>
          </label>
        
        <nav class="md-nav" data-md-level="1" aria-labelledby="__nav_6_label" aria-expanded="false">
          <label class="md-nav__title" for="__nav_6">
            <span class="md-nav__icon md-icon"></span>
            PublishedArticle
          </label>
          <ul class="md-nav__list" data-md-scrollfix>
            
              
                
  
  
  
  
    <li class="md-nav__item">
      <a href="../../PublishedArticle/" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    已发布文章
  </span>
  

      </a>
    </li>
  

              
            
          </ul>
        </nav>
      
    </li>
  

    
      
      
  
  
  
  
    
    
    
    
    <li class="md-nav__item md-nav__item--nested">
      
        
        
        <input class="md-nav__toggle md-toggle " type="checkbox" id="__nav_7" >
        
          
          <label class="md-nav__link" for="__nav_7" id="__nav_7_label" tabindex="0">
            
  
  <span class="md-ellipsis">
    SoftWareUsage
  </span>
  

            <span class="md-nav__icon md-icon"></span>
          </label>
        
        <nav class="md-nav" data-md-level="1" aria-labelledby="__nav_7_label" aria-expanded="false">
          <label class="md-nav__title" for="__nav_7">
            <span class="md-nav__icon md-icon"></span>
            SoftWareUsage
          </label>
          <ul class="md-nav__list" data-md-scrollfix>
            
              
                
  
  
  
  
    <li class="md-nav__item">
      <a href="../../SoftWareUsage/edge%E4%BD%BF%E7%94%A8/" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    Edge使用
  </span>
  

      </a>
    </li>
  

              
            
              
                
  
  
  
  
    <li class="md-nav__item">
      <a href="../../SoftWareUsage/freefileSync%E6%96%87%E4%BB%B6%E5%90%8C%E6%AD%A5%E4%B8%8E%E5%A4%87%E4%BB%BD%E5%B7%A5%E5%85%B7/" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    FreeFileSync文件同步与备份工具
  </span>
  

      </a>
    </li>
  

              
            
              
                
  
  
  
  
    <li class="md-nav__item">
      <a href="../../SoftWareUsage/vs%20%E5%BF%AB%E6%8D%B7%E9%94%AE/" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    Vs 快捷键
  </span>
  

      </a>
    </li>
  

              
            
          </ul>
        </nav>
      
    </li>
  

    
      
      
  
  
  
  
    
    
    
    
    <li class="md-nav__item md-nav__item--nested">
      
        
        
        <input class="md-nav__toggle md-toggle " type="checkbox" id="__nav_8" >
        
          
          <label class="md-nav__link" for="__nav_8" id="__nav_8_label" tabindex="0">
            
  
  <span class="md-ellipsis">
    UniversityStudy
  </span>
  

            <span class="md-nav__icon md-icon"></span>
          </label>
        
        <nav class="md-nav" data-md-level="1" aria-labelledby="__nav_8_label" aria-expanded="false">
          <label class="md-nav__title" for="__nav_8">
            <span class="md-nav__icon md-icon"></span>
            UniversityStudy
          </label>
          <ul class="md-nav__list" data-md-scrollfix>
            
              
                
  
  
  
  
    <li class="md-nav__item">
      <a href="../../UniversityStudy/%E5%A4%A7%E5%AD%A6%E5%AD%A6%E4%B9%A0%E9%93%81%E5%BE%8B/" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    大学学习（至少是大一到大四）永远要干的事情
  </span>
  

      </a>
    </li>
  

              
            
              
                
  
  
  
  
    <li class="md-nav__item">
      <a href="../../UniversityStudy/%E5%A6%82%E4%BD%95%E4%BC%98%E9%9B%85%E7%9A%84%E8%AE%B2%E8%A7%A3%E7%9F%A5%E8%AF%86/" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    如何优雅的讲解知识
  </span>
  

      </a>
    </li>
  

              
            
              
                
  
  
  
  
    <li class="md-nav__item">
      <a href="../../UniversityStudy/%E6%AF%95%E4%B8%9A%E7%AD%94%E8%BE%A9/" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    毕业答辩
  </span>
  

      </a>
    </li>
  

              
            
          </ul>
        </nav>
      
    </li>
  

    
      
      
  
  
  
  
    
    
    
    
    <li class="md-nav__item md-nav__item--nested">
      
        
        
        <input class="md-nav__toggle md-toggle " type="checkbox" id="__nav_9" >
        
          
          <label class="md-nav__link" for="__nav_9" id="__nav_9_label" tabindex="0">
            
  
  <span class="md-ellipsis">
    WindowsOS
  </span>
  

            <span class="md-nav__icon md-icon"></span>
          </label>
        
        <nav class="md-nav" data-md-level="1" aria-labelledby="__nav_9_label" aria-expanded="false">
          <label class="md-nav__title" for="__nav_9">
            <span class="md-nav__icon md-icon"></span>
            WindowsOS
          </label>
          <ul class="md-nav__list" data-md-scrollfix>
            
              
                
  
  
  
  
    <li class="md-nav__item">
      <a href="../../WindowsOS/OS%E7%94%B5%E8%84%91%E6%B8%85%E7%90%86%E4%B8%8E%E7%B3%BB%E7%BB%9F%E9%87%8D%E8%A3%85/" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    装机前的电脑备份工作
  </span>
  

      </a>
    </li>
  

              
            
              
                
  
  
  
  
    <li class="md-nav__item">
      <a href="../../WindowsOS/Windows%E7%A3%81%E7%9B%98%E7%AE%A1%E7%90%86%EF%BC%9A%E5%88%A0%E9%99%A4%E5%88%86%E5%8C%BA/" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    Windows磁盘管理：删除不需要的分区（恢复分区等）
  </span>
  

      </a>
    </li>
  

              
            
              
                
  
  
  
  
    <li class="md-nav__item">
      <a href="../../WindowsOS/Windows%E7%B3%BB%E7%BB%9F%E7%9B%98%E6%96%87%E4%BB%B6%E7%9B%AE%E5%BD%95%E4%BB%8B%E7%BB%8D/" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    Windows系统盘文件目录介绍
  </span>
  

      </a>
    </li>
  

              
            
              
                
  
  
  
  
    <li class="md-nav__item">
      <a href="../../WindowsOS/%5BWindows%5D%E7%B3%BB%E7%BB%9F%E4%BF%9D%E6%8A%A4%EF%BC%9A%E5%8D%A0%E7%94%A8%E7%A3%81%E7%9B%98100G%E7%9A%84%E7%BD%AA%E9%AD%81%E7%A5%B8%E9%A6%96---CD%E7%9B%98%E6%97%A0%E6%95%85%E5%8D%A0%E7%94%A8%E5%86%85%E5%AD%98%28%E7%A1%AC%E7%9B%98%29%E7%A9%BA%E9%97%B4%E5%8E%9F%E5%9B%A0/" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    系统保护:占用磁盘100G的罪魁祸首---C/D盘无故占用内存(硬盘)空间原因
  </span>
  

      </a>
    </li>
  

              
            
              
                
  
  
  
  
    <li class="md-nav__item">
      <a href="../../WindowsOS/regedit/" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    Regedit
  </span>
  

      </a>
    </li>
  

              
            
              
                
  
  
  
  
    <li class="md-nav__item">
      <a href="../../WindowsOS/windows%E5%AD%A6%E4%B9%A0%E4%B8%8E%E4%BD%BF%E7%94%A8/" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    Windows学习与使用
  </span>
  

      </a>
    </li>
  

              
            
              
                
  
  
  
  
    <li class="md-nav__item">
      <a href="../../WindowsOS/windows%E5%BF%AB%E6%8D%B7%E9%94%AE/" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    Windows快捷键
  </span>
  

      </a>
    </li>
  

              
            
              
                
  
  
  
  
    <li class="md-nav__item">
      <a href="../../WindowsOS/%E9%87%8D%E8%A3%85%E7%B3%BB%E7%BB%9F%20%26%20%E5%87%86%E5%A4%87%E5%B7%A5%E4%BD%9C/" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    重装系统 & 重装前的准备工作
  </span>
  

      </a>
    </li>
  

              
            
              
                
  
  
  
  
    <li class="md-nav__item">
      <a href="../../WindowsOS/%E9%9A%90%E8%97%8F%E6%96%87%E4%BB%B6/" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    隐藏文件
  </span>
  

      </a>
    </li>
  

              
            
              
                
  
  
  
  
    
    
    
    
    <li class="md-nav__item md-nav__item--nested">
      
        
        
        <input class="md-nav__toggle md-toggle " type="checkbox" id="__nav_9_10" >
        
          
          <label class="md-nav__link" for="__nav_9_10" id="__nav_9_10_label" tabindex="0">
            
  
  <span class="md-ellipsis">
    清理备份日志
  </span>
  

            <span class="md-nav__icon md-icon"></span>
          </label>
        
        <nav class="md-nav" data-md-level="2" aria-labelledby="__nav_9_10_label" aria-expanded="false">
          <label class="md-nav__title" for="__nav_9_10">
            <span class="md-nav__icon md-icon"></span>
            清理备份日志
          </label>
          <ul class="md-nav__list" data-md-scrollfix>
            
              
                
  
  
  
  
    <li class="md-nav__item">
      <a href="../../WindowsOS/%E6%B8%85%E7%90%86%E5%A4%87%E4%BB%BD%E6%97%A5%E5%BF%97/OS%E7%94%B5%E8%84%91%E6%B8%85%E7%90%86%E4%B8%8E%E7%B3%BB%E7%BB%9F%E9%87%8D%E8%A3%85/" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    装机前的电脑备份工作
  </span>
  

      </a>
    </li>
  

              
            
              
                
  
  
  
  
    <li class="md-nav__item">
      <a href="../../WindowsOS/%E6%B8%85%E7%90%86%E5%A4%87%E4%BB%BD%E6%97%A5%E5%BF%97/%E7%B3%BB%E7%BB%9F%E9%87%8D%E8%A3%85%E4%B8%8B%E8%BD%BD%E7%9A%84%E8%BD%AF%E4%BB%B6%E5%8F%8A%E7%9B%B8%E5%85%B3%E6%93%8D%E4%BD%9C/" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    系统重装下载的软件及相关操作
  </span>
  

      </a>
    </li>
  

              
            
          </ul>
        </nav>
      
    </li>
  

              
            
          </ul>
        </nav>
      
    </li>
  

    
      
      
  
  
  
  
    
    
    
    
    <li class="md-nav__item md-nav__item--nested">
      
        
        
        <input class="md-nav__toggle md-toggle " type="checkbox" id="__nav_10" >
        
          
          <label class="md-nav__link" for="__nav_10" id="__nav_10_label" tabindex="0">
            
  
  <span class="md-ellipsis">
    Utils 实用工具表
  </span>
  

            <span class="md-nav__icon md-icon"></span>
          </label>
        
        <nav class="md-nav" data-md-level="1" aria-labelledby="__nav_10_label" aria-expanded="false">
          <label class="md-nav__title" for="__nav_10">
            <span class="md-nav__icon md-icon"></span>
            Utils 实用工具表
          </label>
          <ul class="md-nav__list" data-md-scrollfix>
            
              
                
  
  
  
  
    <li class="md-nav__item">
      <a href="../../utils_%E5%AE%9E%E7%94%A8%E5%B7%A5%E5%85%B7%E8%A1%A8/ASCII%E7%A0%81%E8%A1%A8/" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    ASCII码表
  </span>
  

      </a>
    </li>
  

              
            
          </ul>
        </nav>
      
    </li>
  

    
      
      
  
  
    
  
  
  
    
    
    
    
    <li class="md-nav__item md-nav__item--active md-nav__item--nested">
      
        
        
        <input class="md-nav__toggle md-toggle " type="checkbox" id="__nav_11" checked>
        
          
          <label class="md-nav__link" for="__nav_11" id="__nav_11_label" tabindex="0">
            
  
  <span class="md-ellipsis">
    常用外设、传感器
  </span>
  

            <span class="md-nav__icon md-icon"></span>
          </label>
        
        <nav class="md-nav" data-md-level="1" aria-labelledby="__nav_11_label" aria-expanded="true">
          <label class="md-nav__title" for="__nav_11">
            <span class="md-nav__icon md-icon"></span>
            常用外设、传感器
          </label>
          <ul class="md-nav__list" data-md-scrollfix>
            
              
                
  
  
  
  
    <li class="md-nav__item">
      <a href="../3%E8%89%B2RGB/" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    跑马灯
  </span>
  

      </a>
    </li>
  

              
            
              
                
  
  
  
  
    <li class="md-nav__item">
      <a href="../DS18b20%E6%B8%A9%E6%B9%BF%E5%BA%A6/" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    快速上手
  </span>
  

      </a>
    </li>
  

              
            
              
                
  
  
  
  
    <li class="md-nav__item">
      <a href="../%E4%B8%B2%E5%8F%A3%E7%9B%B8%E5%85%B3/" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    串口相关
  </span>
  

      </a>
    </li>
  

              
            
              
                
  
  
  
  
    <li class="md-nav__item">
      <a href="../%E4%BF%A1%E5%8F%B7%E7%81%AF%E6%8E%A7%E5%88%B6%E7%B1%BB/" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    合集内容:信号灯控制类
  </span>
  

      </a>
    </li>
  

              
            
              
                
  
  
  
  
    <li class="md-nav__item">
      <a href="../%E5%A4%A7%E5%B0%8F%E7%A3%81%E7%B0%A7_%E7%A3%81%E5%9C%BA%E4%BC%A0%E6%84%9F%E5%99%A8/" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    大小磁簧 磁场传感器
  </span>
  

      </a>
    </li>
  

              
            
              
                
  
  
  
  
    <li class="md-nav__item">
      <a href="../%E5%BE%AA%E8%BF%B9%E6%A8%A1%E5%9D%97/" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    循迹模块
  </span>
  

      </a>
    </li>
  

              
            
              
                
  
  
  
  
    <li class="md-nav__item">
      <a href="../%E6%8C%89%E9%94%AE%E5%BC%80%E5%85%B3/" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    按键开关
  </span>
  

      </a>
    </li>
  

              
            
              
                
  
  
  
  
    <li class="md-nav__item">
      <a href="../%E6%95%B0%E5%AD%97%E6%B8%A9%E5%BA%A6%EF%BC%88%E6%B2%A1%E7%8E%A9%E6%98%8E%E7%99%BD%EF%BC%89/" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    数字温度（没玩明白）
  </span>
  

      </a>
    </li>
  

              
            
              
                
  
  
  
  
    <li class="md-nav__item">
      <a href="../%E6%95%B2%E5%87%BB%E6%A8%A1%E5%9D%97/" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    敲击模块
  </span>
  

      </a>
    </li>
  

              
            
              
                
  
  
  
  
    <li class="md-nav__item">
      <a href="../%E6%B8%B8%E6%88%8F%E6%91%87%E6%9D%86%E6%A8%A1%E5%9D%97/" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    游戏摇杆模块
  </span>
  

      </a>
    </li>
  

              
            
              
                
  
  
  
  
    <li class="md-nav__item">
      <a href="../%E6%BF%80%E5%85%89%E5%8F%91%E5%B0%84%E5%99%A8/" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    激光发射器
  </span>
  

      </a>
    </li>
  

              
            
              
                
  
  
    
  
  
  
    <li class="md-nav__item md-nav__item--active">
      
      <input class="md-nav__toggle md-toggle" type="checkbox" id="__toc">
      
      
        
      
      
      <a href="./" class="md-nav__link md-nav__link--active">
        
  
  <span class="md-ellipsis">
    蜂鸣器类
  </span>
  

      </a>
      
    </li>
  

              
            
              
                
  
  
  
  
    <li class="md-nav__item">
      <a href="../%E8%A7%A6%E6%91%B8%E4%BC%A0%E6%84%9F%E5%99%A8/" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    触摸传感器
  </span>
  

      </a>
    </li>
  

              
            
          </ul>
        </nav>
      
    </li>
  

    
      
      
  
  
  
  
    
    
    
    
    <li class="md-nav__item md-nav__item--nested">
      
        
        
        <input class="md-nav__toggle md-toggle " type="checkbox" id="__nav_12" >
        
          
          <label class="md-nav__link" for="__nav_12" id="__nav_12_label" tabindex="0">
            
  
  <span class="md-ellipsis">
    考研docs（tmp）
  </span>
  

            <span class="md-nav__icon md-icon"></span>
          </label>
        
        <nav class="md-nav" data-md-level="1" aria-labelledby="__nav_12_label" aria-expanded="false">
          <label class="md-nav__title" for="__nav_12">
            <span class="md-nav__icon md-icon"></span>
            考研docs（tmp）
          </label>
          <ul class="md-nav__list" data-md-scrollfix>
            
              
                
  
  
  
  
    <li class="md-nav__item">
      <a href="../../%E8%80%83%E7%A0%94docs%EF%BC%88tmp%EF%BC%89/%E8%80%83%E7%A0%94%E4%B8%AD%E5%9C%BA%E7%9F%AD%E6%9C%9F%E8%A7%84%E5%88%92-%E6%97%B6%E9%97%B4%E8%A1%A8/" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    时间表
  </span>
  

      </a>
    </li>
  

              
            
              
                
  
  
  
  
    <li class="md-nav__item">
      <a href="../../%E8%80%83%E7%A0%94docs%EF%BC%88tmp%EF%BC%89/%E8%80%83%E7%A0%94%E4%B8%AD%E5%9C%BA%E7%9F%AD%E6%9C%9F%E8%A7%84%E5%88%92/" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    考研知识扫盲及短期、长期规划
  </span>
  

      </a>
    </li>
  

              
            
          </ul>
        </nav>
      
    </li>
  

    
  </ul>
</nav>
                  </div>
                </div>
              </div>
            
            
              
              <div class="md-sidebar md-sidebar--secondary" data-md-component="sidebar" data-md-type="toc" >
                <div class="md-sidebar__scrollwrap">
                  <div class="md-sidebar__inner">
                    

<nav class="md-nav md-nav--secondary" aria-label="目录">
  
  
  
    
  
  
</nav>
                  </div>
                </div>
              </div>
            
          
          
            <div class="md-content" data-md-component="content">
              <article class="md-content__inner md-typeset">
                
                  


<blockquote>
<p>前言：蜂鸣器是很好玩滴东西，可以发出各种各样的声音
不过要注意接线！！
本文工作：整理了文档和网上的资料，详细描述了蜂鸣器的发声原理和两种驱动代码实现。自己拟写了三个arduino的demo（门铃和倒车雷达，结合按键模块和串口通信实现）
待修改内容：将有源和无源整合到一起，使文章更流畅（大部分文字都是复制的）</p>
</blockquote>
<h1 id="_1">蜂鸣器的分类<a class="headerlink" href="#_1" title="Permanent link">&para;</a></h1>
<p><strong>蜂鸣器从驱动方式上来分可以分为无源蜂鸣器和有源蜂鸣器。</strong></p>
<p>无源蜂鸣器是没有正负之分的，类似于喇叭，只要在两个腿上加载不同的频率的电信号就可以实现发声，根据不同的频率所发出的声音也是不一样的。</p>
<p>有源蜂鸣器是有正负之分的，只需要在两个腿上加上电压信号就会发声，发出的声音音调单一、频率固定。</p>
<p>有源蜂鸣器比无源蜂鸣器内部多了振荡结构，所以有源蜂鸣器在价格上稍微贵一点。</p>
<p><strong>根据发声原理不同分为电压式蜂鸣器和电磁式蜂鸣器。</strong></p>
<p>电磁式蜂鸣器：在接通电源后，振动膜片周期性的振动发声。需要注意的一点是，发声是在内部有电流产生磁场，使得膜片振动的一瞬间。 如果内部一直是直流电的话我们只能听到很小的一下的声音，如果需要持续的声音（就像我萌以前过生日的那种唱生日快乐歌的小玩意）应该怎么办呢？<strong>需要一个交流的驱动电路就行。最简单的方波就可以驱动。</strong> 这里我们使用的是无源蜂鸣器， 这里的源指的是振荡源，自带BUFF的就是有源，也就是直接同直流电就可以发声，无源则需要外部送入方波信号用于驱动才行。</p>
<p>其他分类可以参考<a href="https://blog.csdn.net/weixin_42645653/article/details/111991949">csdn的这篇文章</a></p>
<h1 id="_2">有源蜂鸣器<a class="headerlink" href="#_2" title="Permanent link">&para;</a></h1>
<p><img alt="" src="https://cdn.jsdelivr.net/gh/dancehole/image@main/codelabs/%E6%9C%89%E6%BA%90%E8%9C%82%E9%B8%A3%E5%99%A8.jpg" /></p>
<ol>
<li>
<p>介绍：相信大家对蜂鸣器都不会陌生，我们在很多场景下都会用到蜂鸣器，大部分都是使用蜂鸣器来做提示或报警，比如按键按下、开始工作、工作结束或是故障等等。这里对单片机在蜂鸣器驱动上的应用作一下描述。</p>
</li>
<li>
<p>驱动方式
   惯用驱动蜂鸣器的方式有两种：
    一种是 PWM 输出口直接驱动，另一种是利用I/O 定时翻转电平产生驱动波形对蜂鸣器进行驱动。</p>
</li>
<li>
<p>PWM输出口直接驱动是利用 PWM 输出口本身可以输出一定的方波来直接驱动蜂鸣器。在单片机的软件设置中有几个系统寄存器是用来设置 PWM 口的输出的，可以设置占空比、周期等等，通过设置这些寄存器产生符合蜂鸣器要求的频率的波形之后，只要打开 PWM 输出，PWM 输出口就能输出该频率的方波，这个时候利用这个波形就可以驱动蜂鸣器了。比如频率为 2000Hz的蜂鸣器的驱动，可以知道周期为 500μs，这样只需要把 PWM 的周期设置为 500μs，占空比电平设置为 250μs，就能产生一个频率为 2000Hz 的方波，通过这个方波再利用三极管就可以去驱动这个蜂鸣器了。</p>
</li>
<li>
<p>利用 I/O 定时翻转电平来产生驱动波形的方式会比较麻烦一点，必须利用定时器来做定时，通过定时翻转电平产生符合蜂鸣器要求的频率的波形，这个波形就可以用来驱动蜂鸣器了。比如为 2500Hz 的蜂鸣器的驱动，可以知道周期为 400μs，这样只需要驱动蜂鸣器的 I/O 口每 200μs 翻转一次电平就可以产生一个频率为2500Hz，占空比为 &frac12;duty 的方波，再通过三极管放大就可以驱动这个蜂鸣器了。
     <img alt="" src="https://cdn.jsdelivr.net/gh/dancehole/image@main/codelabs/%E6%9C%89%E6%BA%90%E8%9C%82%E9%B8%A3%E5%99%A8%E5%8E%9F%E7%90%86%E5%9B%BE.png" /></p>
</li>
</ol>
<p>三、模块使用</p>
<p>大家看看到模块后应该明白，其实他非常方便使用，一个电源端，一个地端，还有一个就是信号输入端。我们只要把电源、地线接好，那信号线接上 IO 口就行</p>
<p>用两种不同的驱动方式驱动蜂鸣器的发声，大家可以比较下效果，一方便以后的使用。
具体的接法下面有:</p>
<table>
<thead>
<tr>
<th style="text-align: center;">模块</th>
<th>主板</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align: center;">+(边)/S</td>
<td>pin8</td>
</tr>
<tr>
<td style="text-align: center;">+(中间)</td>
<td>5v</td>
</tr>
<tr>
<td style="text-align: center;">-(边)</td>
<td>GCC</td>
</tr>
</tbody>
</table>
<p>如图：
<img alt="" src="https://cdn.jsdelivr.net/gh/dancehole/image@main/codelabs/%E6%9C%89%E6%BA%90%E8%9C%82%E9%B8%A3%E5%99%A8%E6%8E%A5%E7%BA%BF.jpg" /></p>
<p>下面的这个测试代码是一个关于使用模拟量控制蜂鸣器显示频率的程序：
程序说明：第 10 引脚为控制蜂鸣器的引脚。
第 3 引脚为模拟量引脚，所使用的可调电阻为 10K。
功能：调动可调电阻器可以听到蜂鸣器响明显的频率变化。</p>
<div class="highlight"><pre><span></span><code><span class="c1">//电位器控制发生频率（愠出一个频率的声音）</span>
<span class="kr">int</span><span class="w"> </span><span class="n">speakerPin</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">8</span><span class="p">;</span><span class="c1">//控制喇叭的引脚</span>
<span class="kr">int</span><span class="w"> </span><span class="n">potPin</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">4</span><span class="p">;</span><span class="c1">//控制可调电阻器的引脚</span>
<span class="kr">int</span><span class="w"> </span><span class="n">value</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="kr">void</span><span class="w"> </span><span class="nb">setup</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nf">pinMode</span><span class="p">(</span><span class="n">speakerPin</span><span class="p">,</span><span class="w"> </span><span class="kr">OUTPUT</span><span class="p">);</span>
<span class="p">}</span>
<span class="kr">void</span><span class="w"> </span><span class="nb">loop</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<span class="w">  </span><span class="n">value</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">analogRead</span><span class="p">(</span><span class="n">potPin</span><span class="p">);</span><span class="n">读电阻器引脚的值</span>
<span class="w"> </span><span class="nf">digitalWrite</span><span class="p">(</span><span class="n">speakerPin</span><span class="p">,</span><span class="w"> </span><span class="kr">HIGH</span><span class="p">);</span>
<span class="w"> </span><span class="nf">delay</span><span class="p">(</span><span class="n">value</span><span class="p">);</span><span class="n">调节喇叭响的时间</span><span class="err">；</span>
<span class="w"> </span><span class="nf">digitalWrite</span><span class="p">(</span><span class="n">speakerPin</span><span class="p">,</span><span class="w"> </span><span class="kr">LOW</span><span class="p">);</span>
<span class="w"> </span><span class="nf">delay</span><span class="p">(</span><span class="n">value</span><span class="p">);</span><span class="n">调节喇叭不响的时间</span><span class="err">；</span>
<span class="p">}</span>
</code></pre></div>
<p>在这里我们可以认为的调节电位器来达到延时不同时间的效果，从而改变蜂鸣器的发声频率。</p>
<p>下面我们加了一个按键开关控制蜂鸣器，这样我们就能模拟一个简单的门铃，当按键被按下时喇叭就可以发出响声了。
<div class="highlight"><pre><span></span><code><span class="c1">//添加按键模块，模拟门铃</span>
<span class="kr">const</span><span class="w"> </span><span class="kr">int</span><span class="w"> </span><span class="n">buttonPin</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">4</span><span class="p">;</span><span class="w"> </span><span class="c1">// 按键引脚;</span>
<span class="kr">const</span><span class="w"> </span><span class="kr">int</span><span class="w"> </span><span class="n">speakerPin</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">8</span><span class="p">;</span><span class="w"> </span><span class="c1">//蜂鸣器引脚;</span>
<span class="c1">// variables will change:</span>
<span class="kr">int</span><span class="w"> </span><span class="n">buttonState</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="c1">// 读取按键引脚的一个值</span>
<span class="kr">void</span><span class="w"> </span><span class="nb">setup</span><span class="p">()</span>
<span class="p">{</span>
<span class="w"> </span><span class="c1">//设置按键引脚为输入模式，蜂鸣器引脚为输出模式；</span>
<span class="w"> </span><span class="nf">pinMode</span><span class="p">(</span><span class="n">speakerPin</span><span class="p">,</span><span class="w"> </span><span class="kr">OUTPUT</span><span class="p">);</span><span class="w"> </span>
<span class="w"> </span><span class="nf">pinMode</span><span class="p">(</span><span class="n">buttonPin</span><span class="p">,</span><span class="w"> </span><span class="kr">INPUT</span><span class="p">);</span><span class="w"> </span>
<span class="p">}</span>
<span class="kr">void</span><span class="w"> </span><span class="nb">loop</span><span class="p">(){</span>
<span class="w"> </span><span class="c1">// 读取按键一个初值，这里在电路中我接了是在默认高电平，所以初值为高；</span>
<span class="w"> </span><span class="n">buttonState</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">digitalRead</span><span class="p">(</span><span class="n">buttonPin</span><span class="p">);</span>
<span class="w"> </span><span class="cm">/* 如果按键为高，则蜂鸣器不响；</span>
<span class="cm">因为我刚开始接在硬件电路中初值为高，所以 if 条件成立，蜂鸣器不响</span>
<span class="cm">*/</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">buttonState</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="kr">HIGH</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span>
<span class="w"> </span><span class="nf">digitalWrite</span><span class="p">(</span><span class="n">speakerPin</span><span class="p">,</span><span class="kr">LOW</span><span class="p">);</span>
<span class="w"> </span><span class="p">}</span><span class="w"> </span>
<span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="c1">//这里按键的值为低电平（也是按键被按下时）；蜂鸣器响起</span>
<span class="w"> </span><span class="nf">digitalWrite</span><span class="p">(</span><span class="n">speakerPin</span><span class="p">,</span><span class="kr">HIGH</span><span class="p">);</span>
<span class="w"> </span><span class="p">}</span>
<span class="p">}</span>
</code></pre></div>
用 PWM 控制蜂鸣器的代码。
下面这个程序是利用 PWM（脉冲宽度调节）控制蜂鸣器的，下载到单片机可以听到蜂鸣器发出不同的音调，我们只要根据相关曲目调节出音符(0,1,2,3,4,5,6,7)就可以让蜂鸣器奏乐了。
程序如下：</p>
<div class="highlight"><pre><span></span><code><span class="c1">//曲目：天空之城</span>
<span class="c1">//接线：信号线为pin8</span>
<span class="cm">/* Author: Waao666</span>
<span class="cm"> * Created on 2020-1-7  </span>
<span class="cm"> * Version 1.0 </span>
<span class="cm"> */</span><span class="w"> </span>
<span class="cm">/* 以上部分是定义是把每个音符和频率值对应起来 */</span>
<span class="cp">#define NOTE_D0 -1 </span>
<span class="cp">#define NOTE_D1 294 </span>
<span class="cp">#define NOTE_D2 330 </span>
<span class="cp">#define NOTE_D3 350 </span>
<span class="cp">#define NOTE_D4 393 </span>
<span class="cp">#define NOTE_D5 441 </span>
<span class="cp">#define NOTE_D6 495 </span>
<span class="cp">#define NOTE_D7 556 </span>

<span class="cp">#define NOTE_DL1 147 </span>
<span class="cp">#define NOTE_DL2 165 </span>
<span class="cp">#define NOTE_DL3 175 </span>
<span class="cp">#define NOTE_DL4 196 </span>
<span class="cp">#define NOTE_DL5 221 </span>
<span class="cp">#define NOTE_DL6 248 </span>
<span class="cp">#define NOTE_DL7 278 </span>

<span class="cp">#define NOTE_DH1 589 </span>
<span class="cp">#define NOTE_DH2 661 </span>
<span class="cp">#define NOTE_DH3 700 </span>
<span class="cp">#define NOTE_DH4 786 </span>
<span class="cp">#define NOTE_DH5 882 </span>
<span class="cp">#define NOTE_DH6 990 </span>
<span class="cp">#define NOTE_DH7 112  </span>
<span class="cm">/**************************************/</span>

<span class="cm">/********** 这部分是用英文对应了拍子 **********/</span>
<span class="cp">#define WHOLE 1 </span>
<span class="cp">#define HALF 0.5 </span>
<span class="cp">#define QUARTER 0.25 </span>
<span class="cp">#define EIGHTH 0.25 </span>
<span class="cp">#define SIXTEENTH 0.625 </span>
<span class="cm">/******************************************/</span>

<span class="cm">/* 这部分就是整首曲子的音符部分,用了一个序列定义为tune，整数 */</span>
<span class="kr">int</span><span class="w"> </span><span class="n">tune</span><span class="p">[]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="w">  </span>
<span class="w"> </span><span class="n">NOTE_D0</span><span class="p">,</span><span class="n">NOTE_D0</span><span class="p">,</span><span class="n">NOTE_D0</span><span class="p">,</span><span class="n">NOTE_D6</span><span class="p">,</span><span class="n">NOTE_D7</span><span class="p">,</span><span class="n">NOTE_DH1</span><span class="p">,</span><span class="n">NOTE_D7</span><span class="p">,</span><span class="n">NOTE_DH1</span><span class="p">,</span><span class="n">NOTE_DH3</span><span class="p">,</span><span class="n">NOTE_D7</span><span class="p">,</span><span class="n">NOTE_D7</span><span class="p">,</span><span class="n">NOTE_D7</span><span class="p">,</span><span class="n">NOTE_D3</span><span class="p">,</span><span class="n">NOTE_D3</span><span class="p">,</span>
<span class="w"> </span><span class="n">NOTE_D6</span><span class="p">,</span><span class="n">NOTE_D5</span><span class="p">,</span><span class="n">NOTE_D6</span><span class="p">,</span><span class="n">NOTE_DH1</span><span class="p">,</span><span class="n">NOTE_D5</span><span class="p">,</span><span class="n">NOTE_D5</span><span class="p">,</span><span class="n">NOTE_D5</span><span class="p">,</span><span class="n">NOTE_D3</span><span class="p">,</span><span class="n">NOTE_D4</span><span class="p">,</span><span class="n">NOTE_D3</span><span class="p">,</span><span class="n">NOTE_D4</span><span class="p">,</span><span class="n">NOTE_DH1</span><span class="p">,</span>
<span class="w"> </span><span class="n">NOTE_D3</span><span class="p">,</span><span class="n">NOTE_D3</span><span class="p">,</span><span class="n">NOTE_D0</span><span class="p">,</span><span class="n">NOTE_DH1</span><span class="p">,</span><span class="n">NOTE_DH1</span><span class="p">,</span><span class="n">NOTE_DH1</span><span class="p">,</span><span class="n">NOTE_D7</span><span class="p">,</span><span class="n">NOTE_D4</span><span class="p">,</span><span class="n">NOTE_D4</span><span class="p">,</span><span class="n">NOTE_D7</span><span class="p">,</span><span class="n">NOTE_D7</span><span class="p">,</span><span class="n">NOTE_D7</span><span class="p">,</span><span class="n">NOTE_D0</span><span class="p">,</span><span class="n">NOTE_D6</span><span class="p">,</span><span class="n">NOTE_D7</span><span class="p">,</span>
<span class="w"> </span><span class="n">NOTE_DH1</span><span class="p">,</span><span class="n">NOTE_D7</span><span class="p">,</span><span class="n">NOTE_DH1</span><span class="p">,</span><span class="n">NOTE_DH3</span><span class="p">,</span><span class="n">NOTE_D7</span><span class="p">,</span><span class="n">NOTE_D7</span><span class="p">,</span><span class="n">NOTE_D7</span><span class="p">,</span><span class="n">NOTE_D3</span><span class="p">,</span><span class="n">NOTE_D3</span><span class="p">,</span><span class="n">NOTE_D6</span><span class="p">,</span><span class="n">NOTE_D5</span><span class="p">,</span><span class="n">NOTE_D6</span><span class="p">,</span><span class="n">NOTE_DH1</span><span class="p">,</span>
<span class="w"> </span><span class="n">NOTE_D5</span><span class="p">,</span><span class="n">NOTE_D5</span><span class="p">,</span><span class="n">NOTE_D5</span><span class="p">,</span><span class="n">NOTE_D2</span><span class="p">,</span><span class="n">NOTE_D3</span><span class="p">,</span><span class="n">NOTE_D4</span><span class="p">,</span><span class="n">NOTE_DH1</span><span class="p">,</span><span class="n">NOTE_D7</span><span class="p">,</span><span class="n">NOTE_D7</span><span class="p">,</span><span class="n">NOTE_DH1</span><span class="p">,</span><span class="n">NOTE_DH1</span><span class="p">,</span><span class="n">NOTE_DH2</span><span class="p">,</span><span class="n">NOTE_DH2</span><span class="p">,</span><span class="n">NOTE_DH3</span><span class="p">,</span><span class="n">NOTE_DH1</span><span class="p">,</span><span class="n">NOTE_DH1</span><span class="p">,</span><span class="n">NOTE_DH1</span><span class="p">,</span>
<span class="w"> </span><span class="n">NOTE_DH1</span><span class="p">,</span><span class="n">NOTE_D7</span><span class="p">,</span><span class="n">NOTE_D6</span><span class="p">,</span><span class="n">NOTE_D6</span><span class="p">,</span><span class="n">NOTE_D7</span><span class="p">,</span><span class="n">NOTE_D5</span><span class="p">,</span><span class="n">NOTE_D6</span><span class="p">,</span><span class="n">NOTE_D6</span><span class="p">,</span><span class="n">NOTE_D6</span><span class="p">,</span><span class="n">NOTE_DH1</span><span class="p">,</span><span class="n">NOTE_DH2</span><span class="p">,</span><span class="n">NOTE_DH3</span><span class="p">,</span><span class="n">NOTE_DH2</span><span class="p">,</span><span class="n">NOTE_DH3</span><span class="p">,</span><span class="n">NOTE_DH5</span><span class="p">,</span>
<span class="w"> </span><span class="n">NOTE_DH2</span><span class="p">,</span><span class="n">NOTE_DH2</span><span class="p">,</span><span class="n">NOTE_DH2</span><span class="p">,</span><span class="n">NOTE_D5</span><span class="p">,</span><span class="n">NOTE_D5</span><span class="p">,</span><span class="n">NOTE_DH1</span><span class="p">,</span><span class="n">NOTE_D7</span><span class="p">,</span><span class="n">NOTE_DH1</span><span class="p">,</span><span class="n">NOTE_DH3</span><span class="p">,</span><span class="n">NOTE_DH3</span><span class="p">,</span><span class="n">NOTE_DH3</span><span class="p">,</span><span class="n">NOTE_DH3</span><span class="p">,</span><span class="n">NOTE_DH3</span><span class="p">,</span>
<span class="w"> </span><span class="n">NOTE_D6</span><span class="p">,</span><span class="n">NOTE_D7</span><span class="p">,</span><span class="n">NOTE_DH1</span><span class="p">,</span><span class="n">NOTE_D7</span><span class="p">,</span><span class="n">NOTE_DH2</span><span class="p">,</span><span class="n">NOTE_DH2</span><span class="p">,</span><span class="n">NOTE_DH1</span><span class="p">,</span><span class="n">NOTE_D5</span><span class="p">,</span><span class="n">NOTE_D5</span><span class="p">,</span><span class="n">NOTE_D5</span><span class="p">,</span><span class="n">NOTE_DH4</span><span class="p">,</span><span class="n">NOTE_DH3</span><span class="p">,</span><span class="n">NOTE_DH2</span><span class="p">,</span><span class="n">NOTE_DH1</span><span class="p">,</span>
<span class="w"> </span><span class="n">NOTE_DH3</span><span class="p">,</span><span class="n">NOTE_DH3</span><span class="p">,</span><span class="n">NOTE_DH3</span><span class="p">,</span><span class="n">NOTE_DH3</span><span class="p">,</span><span class="n">NOTE_DH6</span><span class="p">,</span><span class="n">NOTE_DH6</span><span class="p">,</span><span class="n">NOTE_DH5</span><span class="p">,</span><span class="n">NOTE_DH5</span><span class="p">,</span><span class="n">NOTE_DH3</span><span class="p">,</span><span class="n">NOTE_DH2</span><span class="p">,</span><span class="n">NOTE_DH1</span><span class="p">,</span><span class="n">NOTE_DH1</span><span class="p">,</span><span class="n">NOTE_D0</span><span class="p">,</span><span class="n">NOTE_DH1</span><span class="p">,</span>
<span class="w"> </span><span class="n">NOTE_DH2</span><span class="p">,</span><span class="n">NOTE_DH1</span><span class="p">,</span><span class="n">NOTE_DH2</span><span class="p">,</span><span class="n">NOTE_DH2</span><span class="p">,</span><span class="n">NOTE_DH5</span><span class="p">,</span><span class="n">NOTE_DH3</span><span class="p">,</span><span class="n">NOTE_DH3</span><span class="p">,</span><span class="n">NOTE_DH3</span><span class="p">,</span><span class="n">NOTE_DH3</span><span class="p">,</span><span class="n">NOTE_DH6</span><span class="p">,</span><span class="n">NOTE_DH6</span><span class="p">,</span><span class="n">NOTE_DH5</span><span class="p">,</span><span class="n">NOTE_DH5</span><span class="p">,</span>
<span class="w"> </span><span class="n">NOTE_DH3</span><span class="p">,</span><span class="n">NOTE_DH2</span><span class="p">,</span><span class="n">NOTE_DH1</span><span class="p">,</span><span class="n">NOTE_DH1</span><span class="p">,</span><span class="n">NOTE_D0</span><span class="p">,</span><span class="n">NOTE_DH1</span><span class="p">,</span><span class="n">NOTE_DH2</span><span class="p">,</span><span class="n">NOTE_DH1</span><span class="p">,</span><span class="n">NOTE_DH2</span><span class="p">,</span><span class="n">NOTE_DH2</span><span class="p">,</span><span class="n">NOTE_D7</span><span class="p">,</span><span class="n">NOTE_D6</span><span class="p">,</span><span class="n">NOTE_D6</span><span class="p">,</span><span class="n">NOTE_D6</span><span class="p">,</span><span class="n">NOTE_D6</span><span class="p">,</span><span class="n">NOTE_D7</span>
<span class="p">};</span>
<span class="cm">/****************************************************/</span>

<span class="cm">/* 这部分是整首曲子的接拍部分，也定义个序列duration，浮点（数组的个数和前面音符的个数是一样的） */</span>
<span class="kr">float</span><span class="w"> </span><span class="n">duration</span><span class="p">[]</span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="w">  </span>
<span class="w">  </span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mf">0.5</span><span class="p">,</span><span class="mf">0.5</span><span class="p">,</span><span class="w">     </span><span class="mi">1</span><span class="o">+</span><span class="mf">0.5</span><span class="p">,</span><span class="mf">0.5</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="w">     </span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mf">0.5</span><span class="p">,</span><span class="mf">0.5</span><span class="p">,</span>
<span class="w">  </span><span class="mi">1</span><span class="o">+</span><span class="mf">0.5</span><span class="p">,</span><span class="mf">0.5</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="w">     </span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="w">          </span><span class="mi">1</span><span class="o">+</span><span class="mf">0.5</span><span class="p">,</span><span class="mf">0.5</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span>
<span class="w">  </span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mf">0.5</span><span class="p">,</span><span class="mf">0.5</span><span class="p">,</span><span class="mf">0.5</span><span class="p">,</span><span class="mf">0.5</span><span class="p">,</span><span class="w">    </span><span class="mi">1</span><span class="o">+</span><span class="mf">0.5</span><span class="p">,</span><span class="mf">0.5</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="w">     </span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mf">0.5</span><span class="p">,</span><span class="mf">0.5</span><span class="p">,</span>
<span class="w">  </span><span class="mi">1</span><span class="o">+</span><span class="mf">0.5</span><span class="p">,</span><span class="mf">0.5</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="w">    </span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mf">0.5</span><span class="p">,</span><span class="mf">0.5</span><span class="p">,</span><span class="w">     </span><span class="mi">1</span><span class="o">+</span><span class="mf">0.5</span><span class="p">,</span><span class="mf">0.5</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span>
<span class="w">  </span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mf">0.5</span><span class="p">,</span><span class="mf">0.5</span><span class="p">,</span><span class="w">    </span><span class="mi">1</span><span class="p">,</span><span class="mf">0.5</span><span class="p">,</span><span class="mf">0.25</span><span class="p">,</span><span class="mf">0.25</span><span class="p">,</span><span class="mf">0.25</span><span class="p">,</span><span class="mf">0.5</span><span class="p">,</span><span class="w">    </span><span class="mf">0.5</span><span class="p">,</span><span class="mf">0.5</span><span class="p">,</span><span class="mf">0.5</span><span class="p">,</span><span class="mf">0.25</span><span class="p">,</span><span class="mf">0.5</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span>
<span class="w">  </span><span class="mf">0.5</span><span class="p">,</span><span class="mf">0.5</span><span class="p">,</span><span class="mf">0.5</span><span class="p">,</span><span class="mf">0.5</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="w">    </span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mf">0.5</span><span class="p">,</span><span class="mf">0.5</span><span class="p">,</span><span class="w">    </span><span class="mi">1</span><span class="o">+</span><span class="mf">0.5</span><span class="p">,</span><span class="mf">0.5</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span>
<span class="w">  </span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mf">0.5</span><span class="p">,</span><span class="mf">0.5</span><span class="p">,</span><span class="w">    </span><span class="mf">1.5</span><span class="p">,</span><span class="mf">0.5</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="w">    </span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span>
<span class="w">  </span><span class="mf">0.5</span><span class="p">,</span><span class="mf">0.5</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mf">0.5</span><span class="p">,</span><span class="mf">0.5</span><span class="p">,</span><span class="w">    </span><span class="mf">1.5</span><span class="p">,</span><span class="mf">0.25</span><span class="p">,</span><span class="mf">0.5</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="w">    </span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span>
<span class="w">  </span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="w">    </span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="w">    </span><span class="mf">0.5</span><span class="p">,</span><span class="mf">0.5</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mf">0.5</span><span class="p">,</span><span class="mf">0.5</span><span class="p">,</span>
<span class="w">  </span><span class="mi">1</span><span class="p">,</span><span class="mf">0.5</span><span class="p">,</span><span class="mf">0.5</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="w">    </span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="w">    </span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span>
<span class="w">  </span><span class="mf">0.5</span><span class="p">,</span><span class="mf">0.5</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mf">0.5</span><span class="p">,</span><span class="mf">0.5</span><span class="p">,</span><span class="w">    </span><span class="mi">1</span><span class="p">,</span><span class="mf">0.5</span><span class="p">,</span><span class="mf">0.25</span><span class="p">,</span><span class="mf">0.5</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="w">    </span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mf">0.5</span><span class="p">,</span><span class="mf">0.5</span>
<span class="p">};</span>
<span class="cm">/**********************************************************************************/</span>

<span class="kr">int</span><span class="w"> </span><span class="n">length</span><span class="p">;</span><span class="w">     </span><span class="cm">/* 这里定义一个变量，后面用来表示共有多少个音符 */</span>
<span class="kr">int</span><span class="w"> </span><span class="n">tonePin</span><span class="o">=</span><span class="mi">12</span><span class="p">;</span><span class="w">  </span><span class="cm">/* 蜂鸣器的pin */</span>

<span class="kr">void</span><span class="w"> </span><span class="nb">setup</span><span class="p">()</span><span class="w"> </span>
<span class="p">{</span>
<span class="w">  </span><span class="cm">/* 设置蜂鸣器的pin为输出模式 */</span>
<span class="w">  </span><span class="nf">pinMode</span><span class="p">(</span><span class="n">tonePin</span><span class="p">,</span><span class="kr">OUTPUT</span><span class="p">);</span>
<span class="w">  </span><span class="cm">/* 这里用了一个sizeof函数， 可以查出tone序列里有多少个音符 */</span>
<span class="w">  </span><span class="n">length</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kr">sizeof</span><span class="p">(</span><span class="n">tune</span><span class="p">)</span><span class="o">/</span><span class="kr">sizeof</span><span class="p">(</span><span class="n">tune</span><span class="p">[</span><span class="mi">0</span><span class="p">]);</span>
<span class="p">}</span><span class="w"> </span>

<span class="kr">void</span><span class="w"> </span><span class="nb">loop</span><span class="p">()</span><span class="w"> </span>
<span class="p">{</span>
<span class="w">  </span><span class="cm">/* 循环音符的次数 */</span>
<span class="w">  </span><span class="k">for</span><span class="p">(</span><span class="kr">int</span><span class="w"> </span><span class="n">x</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">x</span><span class="o">&lt;</span><span class="n">length</span><span class="p">;</span><span class="n">x</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">    </span><span class="cm">/* 此函数依次播放tune序列里的数组，即每个音符 */</span><span class="w"> </span>
<span class="w">    </span><span class="nf">tone</span><span class="p">(</span><span class="n">tonePin</span><span class="p">,</span><span class="n">tune</span><span class="p">[</span><span class="n">x</span><span class="p">]);</span>
<span class="w">    </span><span class="cm">/* 每个音符持续的时间，即节拍duration，是调整时间的越大，曲子速度越慢 */</span>
<span class="w">    </span><span class="nf">delay</span><span class="p">(</span><span class="mi">400</span><span class="o">*</span><span class="n">duration</span><span class="p">[</span><span class="n">x</span><span class="p">]);</span>
<span class="w">    </span><span class="cm">/* 停止当前音符，进入下一音符 */</span>
<span class="w">    </span><span class="nf">noTone</span><span class="p">(</span><span class="n">tonePin</span><span class="p">);</span>
<span class="w">  </span><span class="p">}</span>
<span class="w">  </span><span class="cm">/* 等待5秒后，循环重新开始 */</span>
<span class="w">  </span><span class="nf">delay</span><span class="p">(</span><span class="mi">5000</span><span class="p">);</span>
<span class="p">}</span>
</code></pre></div>
<h1 id="_3">无源蜂鸣器<a class="headerlink" href="#_3" title="Permanent link">&para;</a></h1>
<p>样子长这样：
<img alt="" src="https://cdn.jsdelivr.net/gh/dancehole/image@main/codelabs/%E6%97%A0%E6%BA%90%E8%9C%82%E9%B8%A3%E5%99%A8.jpg" /></p>
<p>用 Arduino 可以完成的互动作品有很多，最常见也最常用的就是声光展示了，前面一直都是在用 LED 小灯在做实验，本个实验就让大家的电路发出声音，能够发出声音的最常见的元器件就是蜂鸣器和喇叭了，两者相比较蜂鸣器更简单和易用所以我们本实验采用蜂鸣器。</p>
<p>蜂鸣器及其原理
（一）蜂鸣器的介绍
1. 蜂鸣器的作用 蜂鸣器是一种一体化结构的电子讯响器，采用直流电压供电，广泛应用于计算机、打印机、复印机、报警器、电子玩具、汽车电子设备、电话机、定时器等电子产品中作发声器件。
2. 蜂鸣器的分类 蜂鸣器主要分为压电式蜂鸣器和电磁式蜂鸣器两种类型。
3. 蜂鸣器的电路图形符号 蜂鸣器在电路中用字母“H”或“HA”（旧标准用“FM”、“LB”、“JD”等）表示。
（二）蜂鸣器的结构原理
1. 压电式蜂鸣器 压电式蜂鸣器主要由多谐振荡器、压电蜂鸣片、阻抗匹配器及共鸣箱、外壳等组成。有的压电式蜂鸣器外壳上还装有发光二极管。多谐振荡器由晶体管或集成电路构成。当接通电源后（1.5~15V 直流工作电压）,多谐振荡器起振,输出 1.5~2.5kHZ 的音频信号，阻抗匹配器推动压电蜂鸣片发声。压电蜂鸣片由锆钛酸铅或铌镁酸铅压电陶瓷材料制成。在陶瓷片的两面镀上银电极经极化和老化处理后，再与黄铜片或不锈钢片粘在一起。
2. 电磁式蜂鸣器 电磁式蜂鸣器由振荡器、电磁线圈、磁铁、振动膜片及外壳等组成。接通电源后，振荡器产生的音频信号电流通过电磁线圈，使电磁线圈产生磁场。振动膜片在电磁线圈和磁铁的相互作用下，周期性地振动发声。有源蜂鸣器与无源蜂鸣器有什么区别
这里的“源”不是指电源。而是指震荡源。也就是说，有源蜂鸣器内部带震荡源，所以只要一通电就会叫。
而无源内部不带震荡源，所以如果用直流信号无法令其鸣叫。必须用 2K~5K 的方波去驱动它。</p>
<p>有源蜂鸣器往往比无源的贵，就是因为里面多个震荡电路。
无源蜂鸣器的优点是：1．便宜，2．声音频率可控，可以做出“多来米发索拉西”的效果。3．在一些特例中，可以和 LED 复用一个控制口 有源蜂鸣器的优点是：程序控制方便 。</p>
<p>ARDUINO 参考源程序：
<div class="highlight"><pre><span></span><code><span class="kr">int</span><span class="w"> </span><span class="n">buzzer</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">8</span><span class="p">;</span><span class="w">  </span><span class="c1">//设置控制蜂鸣器的数字 IO 脚</span>
<span class="kr">void</span><span class="w"> </span><span class="nb">setup</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<span class="w">  </span><span class="nf">pinMode</span><span class="p">(</span><span class="n">buzzer</span><span class="p">,</span><span class="w"> </span><span class="kr">OUTPUT</span><span class="p">);</span><span class="w">  </span><span class="c1">//设置数字 IO 脚模式，OUTPUT 为辒出</span>
<span class="p">}</span>
<span class="kr">void</span><span class="w"> </span><span class="nb">loop</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<span class="w">  </span><span class="kr">unsigned</span><span class="w"> </span><span class="kr">char</span><span class="w"> </span><span class="n">i</span><span class="p">,</span><span class="w"> </span><span class="n">j</span><span class="p">;</span><span class="w">  </span><span class="c1">//定义变量</span>
<span class="w">  </span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="mi">1</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">    </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="mi">80</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w">  </span><span class="c1">//辒出一个频率的声音</span>
<span class="w">    </span><span class="p">{</span>
<span class="w">      </span><span class="nf">digitalWrite</span><span class="p">(</span><span class="n">buzzer</span><span class="p">,</span><span class="w"> </span><span class="kr">HIGH</span><span class="p">);</span><span class="w">  </span><span class="c1">//发声音</span>
<span class="w">      </span><span class="nf">delay</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span><span class="w">                    </span><span class="c1">//延时 1ms</span>
<span class="w">      </span><span class="nf">digitalWrite</span><span class="p">(</span><span class="n">buzzer</span><span class="p">,</span><span class="w"> </span><span class="kr">LOW</span><span class="p">);</span><span class="w">   </span><span class="c1">//不发声音</span>
<span class="w">      </span><span class="nf">delay</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span><span class="w">                    </span><span class="c1">//延时 ms</span>
<span class="w">    </span><span class="p">}</span>
<span class="w">    </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="mi">100</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w">  </span><span class="c1">//辒出另一个频率癿声音</span>
<span class="w">    </span><span class="p">{</span>
<span class="w">      </span><span class="nf">digitalWrite</span><span class="p">(</span><span class="n">buzzer</span><span class="p">,</span><span class="w"> </span><span class="kr">HIGH</span><span class="p">);</span><span class="w">  </span><span class="c1">//发声音</span>
<span class="w">      </span><span class="nf">delay</span><span class="p">(</span><span class="mi">2</span><span class="p">);</span><span class="w">                    </span><span class="c1">//延时 2ms</span>
<span class="w">      </span><span class="nf">digitalWrite</span><span class="p">(</span><span class="n">buzzer</span><span class="p">,</span><span class="w"> </span><span class="kr">LOW</span><span class="p">);</span><span class="w">   </span><span class="c1">//不发声音</span>
<span class="w">      </span><span class="nf">delay</span><span class="p">(</span><span class="mi">2</span><span class="p">);</span><span class="w">                    </span><span class="c1">//延时 2ms</span>
<span class="w">    </span><span class="p">}</span>
<span class="w">  </span><span class="p">}</span>
<span class="p">}</span>
</code></pre></div></p>
<p>判断一个蜂鸣器是有源or无源：给一个直流信号，看会不会发出声音。
代码如下：
<div class="highlight"><pre><span></span><code><span class="kr">void</span><span class="w"> </span><span class="nb">setup</span><span class="p">(){</span>
<span class="w">  </span><span class="nf">pinMode</span><span class="p">(</span><span class="mi">9</span><span class="p">,</span><span class="kr">OUTPUT</span><span class="p">);</span>
<span class="p">}</span>

<span class="kr">void</span><span class="w"> </span><span class="nb">loop</span><span class="p">(){</span>
<span class="w">  </span><span class="nf">digitalWrite</span><span class="p">(</span><span class="mi">9</span><span class="p">,</span><span class="kr">HIGH</span><span class="p">);</span>
<span class="p">}</span>
</code></pre></div>
正确接线后，发出声音的是有源蜂鸣器，因其内部自带震荡源，用直流就可以驱动</p>
<h1 id="_4">原理讲解<a class="headerlink" href="#_4" title="Permanent link">&para;</a></h1>
<blockquote>
<p>来自百度</p>
</blockquote>
<h2 id="_5">驱动电路<a class="headerlink" href="#_5" title="Permanent link">&para;</a></h2>
<p>由于蜂鸣器的工作电流一般比较大，以致于单片机的I/O 口是无法直接驱动的*（但AVR可以驱动小功率蜂鸣器），所以要利用放大电路来驱动，一般使用三极管来放大电流就可以了。</p>
<p>蜂鸣器驱动电路一般都包含以下几个部分：一个三极管、一个蜂鸣器、一个续流二极管和一个电源滤波电容。</p>
<p>1．蜂鸣器
　　发声元件，在其两端施加直流电压（有源蜂鸣器）或者方波（无源蜂鸣器）就可以发声，其主要参数是外形尺寸、发声方向、工作电压、工作频率、工作电流、驱动方式（直流/方波）等。这些都可以根据需要来选择。</p>
<p>2．续流二极管
　　蜂鸣器本质上是一个感性元件，其电流不能瞬变，因此必须有一个续流二极管提供续流。否则，在蜂鸣器两端会产生几十伏的尖峰电压，可能损坏驱动三极管，并干扰整个电路系统的其它部分。</p>
<p>3．滤波电容
　　滤波电容C1的作用是滤波，滤除蜂鸣器电流对其它部分的影响，也可改善电源的交流阻抗，如果可能，最好是再并联一个220uF的电解电容。</p>
<p>4．三极管
　　三极管Q1起开关作用，其基极的高电平使三极管饱和导通，使蜂鸣器发声；而基极低电平则使三极管关闭，蜂鸣器停止发声。</p>
<p>驱动设计
　　由于这里要介绍两种驱动方式的方法，所以在设计模块系统中将两种驱动方式做到一块，即程序里边不仅介绍了PWM，输出口驱动蜂鸣器的方法，还要介绍I/O口驱动蜂鸣器的方法。所以，我们将设计如下的一个系统来说明单片机对蜂鸣器的驱动：系统有两个他激蜂鸣器，频率都为2000Hz，一个由I/O口进行控制，另一个由PWM 输出口进行控制；系统还有两个按键，一个按键为PORT按键，I/O 口控制的蜂鸣器不鸣叫时按一次按键I/O口控制的蜂鸣器鸣叫，再按一次停止鸣叫，另一个按键为PWM 按键，PWM口控制的蜂鸣器不鸣叫时按一次按键PWM输出口控制的蜂鸣器鸣叫，再按一次停止鸣叫。</p>
<p>这里介绍arduino的一个函数：tone();
参考：<a href="http://www.taichi-maker.com/homepage/reference-index/arduino-code-reference/tone/">tone()函数说明</a></p>
<p>tone()函数可以产生固定频率的PWM信号来驱动扬声器发声。发声时间长度和声调都可以通过参数控制。定义发声时间长度有两种方法，第一种是通过tone()函数的参数来定义发声时长，另一种是使用noTone()函数来停止发声。如果您在使用tone()函数时没有定义发声时间长度，那么除非您通过noTone()函数来停止声音，否则Arduino将会一直通过tone()函数产生声音信号。</p>
<p>Arduino一次只能产生一个声音。假如Arduino的某一个引脚正在通过tone()函数产生发声信号，那么此时让Arduino使用另外一个引脚通过tone()函数发声是不行的。</p>
<p>语法
- <code>tone(pin, frequency)</code>
- <code>tone(pin, frequency, duration)</code></p>
<p>参数
- pin: 发声引脚（该引脚需要连接扬声器）
- frequency: 发声频率（单位：赫兹） – 无符号整数型
- duration: 发声时长（单位：ms，此参数为可选参数) – 无符号长整型</p>
<p>返回值
- 无</p>
<p>蜂鸣器对应音符 
<img alt="" src="https://cdn.jsdelivr.net/gh/dancehole/image@main/codelabs/%E8%9C%82%E9%B8%A3%E5%99%A8%E9%A2%91%E7%8E%87%E5%AF%B9%E5%BA%94%E9%9F%B3%E7%AC%A6.jpg" /></p>
<p>发声可以参考<a href="https://blog.csdn.net/I_AM_JOKER/article/details/104223593">天空之城</a></p>
<p>使用tone()样例：发声</p>
<div class="highlight"><pre><span></span><code><span class="kr">int</span><span class="w"> </span><span class="n">tune</span><span class="p">[]{</span>
<span class="w">  </span><span class="mi">262</span><span class="p">,</span><span class="mi">294</span><span class="p">,</span><span class="mi">330</span><span class="p">,</span><span class="mi">349</span><span class="p">,</span><span class="mi">392</span><span class="p">,</span><span class="mi">440</span><span class="p">,</span><span class="mi">494</span><span class="p">,</span><span class="w">    </span><span class="c1">//b1-b7</span>
<span class="w">  </span><span class="mi">523</span><span class="p">,</span><span class="mi">587</span><span class="p">,</span><span class="mi">659</span><span class="p">,</span><span class="mi">698</span><span class="p">,</span><span class="mi">784</span><span class="p">,</span><span class="mi">880</span><span class="p">,</span><span class="mi">988</span><span class="p">,</span><span class="w">    </span><span class="c1">//c1-c7</span>
<span class="w">  </span><span class="mi">1047</span><span class="p">,</span><span class="mi">1175</span><span class="p">,</span><span class="mi">1319</span><span class="p">,</span><span class="mi">1397</span><span class="p">,</span><span class="mi">1568</span><span class="p">,</span><span class="mi">1760</span><span class="p">,</span><span class="mi">1967</span><span class="w">  </span><span class="c1">//d1-d7</span>
<span class="p">};</span>


<span class="kr">void</span><span class="w"> </span><span class="nb">setup</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>

<span class="p">}</span>

<span class="kr">void</span><span class="w"> </span><span class="nb">loop</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<span class="w">  </span><span class="k">for</span><span class="p">(</span><span class="kr">int</span><span class="w"> </span><span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="mi">27</span><span class="p">;</span><span class="n">i</span><span class="o">++</span><span class="p">){</span>
<span class="w">    </span><span class="nf">tone</span><span class="p">(</span><span class="mi">9</span><span class="p">,</span><span class="n">tune</span><span class="p">[</span><span class="n">i</span><span class="p">],</span><span class="mi">600</span><span class="p">);</span>
<span class="w">    </span><span class="nf">delay</span><span class="p">(</span><span class="mi">1000</span><span class="p">);</span>
<span class="w">  </span><span class="p">}</span>
<span class="p">}</span>
</code></pre></div>
<p>DEMO:模拟倒车雷达（串口方式，由于超声波传感器不可用，暂时用串口代替）</p>
<p>程序功能：输入一个串口数据dis，模拟距离，根据距离模拟倒车（若输入bell(b)，则模拟喇叭/门铃,响一下）
<div class="highlight"><pre><span></span><code><span class="kr">int</span><span class="w"> </span><span class="n">pinBuzzer</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">9</span><span class="p">;</span><span class="w"> </span><span class="c1">//管脚D3连接到蜂鸣器元件的基极</span>
<span class="kr">int</span><span class="w"> </span><span class="n">dis</span><span class="p">;</span><span class="w">  </span><span class="c1">//模拟距离</span>

<span class="kr">void</span><span class="w"> </span><span class="nb">setup</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<span class="w">  </span><span class="nf">pinMode</span><span class="p">(</span><span class="n">pinBuzzer</span><span class="p">,</span><span class="w"> </span><span class="kr">OUTPUT</span><span class="p">);</span><span class="w"> </span><span class="c1">//设置pinBuzzer脚为输出状态</span>
<span class="w">  </span><span class="nf">Serial</span><span class="p">.</span><span class="nf">begin</span><span class="p">(</span><span class="mi">9600</span><span class="p">);</span>
<span class="p">}</span>

<span class="c1">// int ringdelay[]{        </span>
<span class="c1">//   100,100,100,100,100,100,100,100</span>
<span class="c1">// };</span>

<span class="c1">// int silentdelay[]{</span>
<span class="c1">//   2000,1000,500,250,100,50,30,20</span>
<span class="c1">// };</span>

<span class="c1">// int pwn[]{</span>
<span class="c1">//   0,31,63,127,255,255,255</span>
<span class="c1">// };</span>

<span class="c1">// void loop() {     //用pwn信号发出一个固定频率的声音（0-255）</span>
<span class="c1">//   for(int i=0;i&lt;25;i++){</span>
<span class="c1">//     analogWrite(9, 10*i);</span>
<span class="c1">//     delay(100);</span>
<span class="c1">//     digitalWrite(9, 0);</span>
<span class="c1">//     delay(100);</span>
<span class="c1">//   }</span>
<span class="c1">// }</span>

<span class="w"> </span><span class="c1">//发出没有频率的声音，即io翻转输入，仅限于有源蜂鸣器</span>
<span class="c1">// void loop() { </span>
<span class="c1">//   for(int i=0;i&lt;8;i++){</span>
<span class="c1">//     for(int j=0;j&lt;2+i*i;j++){</span>
<span class="c1">//       digitalWrite(9, 1);</span>
<span class="c1">//       delay(ringdelay[i]);</span>

<span class="c1">//       digitalWrite(9, 0);</span>
<span class="c1">//       delay(silentdelay[i]);</span>
<span class="c1">//     }</span>
<span class="c1">//   }</span>
<span class="c1">// }</span>

<span class="kr">void</span><span class="w"> </span><span class="nf">bell</span><span class="p">(</span><span class="kr">int</span><span class="w"> </span><span class="n">dis</span><span class="o">=</span><span class="mi">-1</span><span class="p">){</span><span class="w">      </span><span class="c1">//传入dis范围为dis&gt;0</span>
<span class="w">  </span><span class="k">if</span><span class="p">(</span><span class="n">dis</span><span class="o">==</span><span class="mi">-1</span><span class="p">){</span><span class="w">   </span><span class="c1">//响一下，即喇叭或门铃</span>
<span class="w">    </span><span class="nf">Serial</span><span class="p">.</span><span class="nf">println</span><span class="p">(</span><span class="s">&quot;active bell ring!&quot;</span><span class="p">);</span>
<span class="w">    </span><span class="nf">tone</span><span class="p">(</span><span class="mi">9</span><span class="p">,</span><span class="mi">2000</span><span class="p">,</span><span class="mi">250</span><span class="p">);</span><span class="w">     </span><span class="c1">//发声方法1：pwn驱动：可以调整频率</span>
<span class="w">    </span><span class="nf">delay</span><span class="p">(</span><span class="mi">250</span><span class="p">);</span>
<span class="w">    </span><span class="nf">tone</span><span class="p">(</span><span class="mi">9</span><span class="p">,</span><span class="mi">800</span><span class="p">,</span><span class="mi">400</span><span class="p">);</span>
<span class="w">    </span><span class="nf">delay</span><span class="p">(</span><span class="mi">500</span><span class="p">);</span>
<span class="w">  </span><span class="p">}</span>
<span class="w">  </span><span class="k">else</span><span class="p">{</span><span class="w">     </span>
<span class="w">    </span><span class="nf">Serial</span><span class="p">.</span><span class="nf">println</span><span class="p">(</span><span class="s">&quot;active car ring ! distance = &quot;</span><span class="p">);</span>
<span class="w">    </span><span class="nf">Serial</span><span class="p">.</span><span class="nf">println</span><span class="p">(</span><span class="n">dis</span><span class="p">);</span>
<span class="w">    </span><span class="kr">int</span><span class="w"> </span><span class="n">delaytime</span><span class="o">=</span><span class="mi">100</span><span class="o">+</span><span class="mi">20</span><span class="o">*</span><span class="n">dis</span><span class="p">;</span>
<span class="w">    </span><span class="k">for</span><span class="p">(</span><span class="kr">int</span><span class="w"> </span><span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;</span><span class="mi">5</span><span class="p">;</span><span class="n">i</span><span class="o">++</span><span class="p">){</span><span class="w">       </span><span class="c1">//发声方法2：io翻转，难以调整频率，无源蜂鸣器难以发声</span>
<span class="w">      </span><span class="nf">digitalWrite</span><span class="p">(</span><span class="mi">9</span><span class="p">,</span><span class="mi">1</span><span class="p">);</span>
<span class="w">      </span><span class="nf">delay</span><span class="p">(</span><span class="n">delaytime</span><span class="p">);</span>
<span class="w">      </span><span class="nf">digitalWrite</span><span class="p">(</span><span class="mi">9</span><span class="p">,</span><span class="mi">0</span><span class="p">);</span>
<span class="w">      </span><span class="nf">delay</span><span class="p">(</span><span class="n">delaytime</span><span class="p">);</span>
<span class="w">    </span><span class="p">}</span>
<span class="w">  </span><span class="p">}</span>
<span class="p">}</span>

<span class="kr">void</span><span class="w"> </span><span class="nb">loop</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>

<span class="w">  </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nf">Serial</span><span class="p">.</span><span class="nf">available</span><span class="p">()</span><span class="w"> </span><span class="o">&gt;</span><span class="w"> </span><span class="mi">0</span><span class="p">)</span><span class="w">   </span><span class="c1">//串口若有输入数据，则读取并覆盖</span>
<span class="w">  </span><span class="p">{</span>
<span class="w">    </span><span class="n">dis</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nf">Serial</span><span class="p">.</span><span class="nf">parseInt</span><span class="p">();</span><span class="c1">//获取串口接收到的数据</span>
<span class="w">    </span><span class="nf">Serial</span><span class="p">.</span><span class="nf">println</span><span class="p">(</span><span class="n">dis</span><span class="p">);</span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">dis</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">-1</span><span class="p">)</span>
<span class="w">    </span><span class="p">{</span>
<span class="w">      </span><span class="n">bell</span><span class="p">();</span>
<span class="w">    </span><span class="p">}</span>
<span class="w">    </span><span class="k">else</span><span class="w"> </span><span class="p">{</span>
<span class="w">      </span><span class="n">bell</span><span class="p">(</span><span class="n">dis</span><span class="p">);</span>

<span class="w">      </span><span class="p">}</span>
<span class="w">  </span><span class="p">}</span>
<span class="w">  </span><span class="k">else</span><span class="p">{</span>
<span class="w">    </span><span class="nf">Serial</span><span class="p">.</span><span class="nf">println</span><span class="p">(</span><span class="s">&quot;no match distance!&quot;</span><span class="p">);</span>
<span class="w">  </span><span class="p">}</span>
<span class="w">  </span><span class="nf">delay</span><span class="p">(</span><span class="mi">1000</span><span class="p">);</span>

<span class="p">}</span>
</code></pre></div></p>







  
    
  
  
    
  


  <aside class="md-source-file">
    
      
  <span class="md-source-file__fact">
    <span class="md-icon" title="最后更新">
      <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M21 13.1c-.1 0-.3.1-.4.2l-1 1 2.1 2.1 1-1c.2-.2.2-.6 0-.8l-1.3-1.3c-.1-.1-.2-.2-.4-.2m-1.9 1.8-6.1 6V23h2.1l6.1-6.1-2.1-2M12.5 7v5.2l4 2.4-1 1L11 13V7h1.5M11 21.9c-5.1-.5-9-4.8-9-9.9C2 6.5 6.5 2 12 2c5.3 0 9.6 4.1 10 9.3-.3-.1-.6-.2-1-.2s-.7.1-1 .2C19.6 7.2 16.2 4 12 4c-4.4 0-8 3.6-8 8 0 4.1 3.1 7.5 7.1 7.9l-.1.2v1.8Z"/></svg>
    </span>
    <span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-date">March 7, 2024</span>
  </span>

    
    
      
  <span class="md-source-file__fact">
    <span class="md-icon" title="创建日期">
      <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M14.47 15.08 11 13V7h1.5v5.25l3.08 1.83c-.41.28-.79.62-1.11 1m-1.39 4.84c-.36.05-.71.08-1.08.08-4.42 0-8-3.58-8-8s3.58-8 8-8 8 3.58 8 8c0 .37-.03.72-.08 1.08.69.1 1.33.32 1.92.64.1-.56.16-1.13.16-1.72 0-5.5-4.5-10-10-10S2 6.5 2 12s4.47 10 10 10c.59 0 1.16-.06 1.72-.16-.32-.59-.54-1.23-.64-1.92M18 15v3h-3v2h3v3h2v-3h3v-2h-3v-3h-2Z"/></svg>
    </span>
    <span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-date">March 7, 2024</span>
  </span>

    
    
    
  </aside>





                
              </article>
            </div>
          
          
<script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script>
        </div>
        
          <button type="button" class="md-top md-icon" data-md-component="top" hidden>
  
  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8v12Z"/></svg>
  回到页面顶部
</button>
        
      </main>
      
        <footer class="md-footer">
  
    
      
      <nav class="md-footer__inner md-grid" aria-label="页脚" >
        
          
          <a href="../%E6%BF%80%E5%85%89%E5%8F%91%E5%B0%84%E5%99%A8/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 激光发射器">
            <div class="md-footer__button md-icon">
              
              <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
            </div>
            <div class="md-footer__title">
              <span class="md-footer__direction">
                上一页
              </span>
              <div class="md-ellipsis">
                激光发射器
              </div>
            </div>
          </a>
        
        
          
          <a href="../%E8%A7%A6%E6%91%B8%E4%BC%A0%E6%84%9F%E5%99%A8/" class="md-footer__link md-footer__link--next" aria-label="下一页: 触摸传感器">
            <div class="md-footer__title">
              <span class="md-footer__direction">
                下一页
              </span>
              <div class="md-ellipsis">
                触摸传感器
              </div>
            </div>
            <div class="md-footer__button md-icon">
              
              <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4Z"/></svg>
            </div>
          </a>
        
      </nav>
    
  
  <div class="md-footer-meta md-typeset">
    <div class="md-footer-meta__inner md-grid">
      <div class="md-copyright">
  
    <div class="md-copyright__highlight">
      Copyright © dancehole邓仕昊 2023 - 2024 深圳技术大学
    </div>
  
  
    Made with
    <a href="https://squidfunk.github.io/mkdocs-material/" target="_blank" rel="noopener">
      Material for MkDocs
    </a>
  
</div>
      
        <div class="md-social">
  
    
    
    
    
      
      
    
    <a href="https://github.com/dancehole" target="_blank" rel="noopener" title="github.com" class="md-social__link">
      <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 496 512"><!--! Font Awesome Free 6.5.1 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2023 Fonticons, Inc.--><path d="M165.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6zm-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3zm44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9zM244.8 8C106.1 8 0 113.3 0 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C428.2 457.8 496 362.9 496 252 496 113.3 383.5 8 244.8 8zM97.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1zm-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7zm32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1zm-11.4-14.7c-1.6 1-1.6 3.6 0 5.9 1.6 2.3 4.3 3.3 5.6 2.3 1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2z"/></svg>
    </a>
  
    
    
    
    
    <a href="mailto:<1391755954@qq.com>" target="_blank" rel="noopener" title="" class="md-social__link">
      <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--! Font Awesome Free 6.5.1 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2023 Fonticons, Inc.--><path d="M498.1 5.6c10.1 7 15.4 19.1 13.5 31.2l-64 416c-1.5 9.7-7.4 18.2-16 23s-18.9 5.4-28 1.6L284 427.7l-68.5 74.1c-8.9 9.7-22.9 12.9-35.2 8.1S160 493.2 160 480v-83.6c0-4 1.5-7.8 4.2-10.7l167.6-182.9c5.8-6.3 5.6-16-.4-22s-15.7-6.4-22-.7L106 360.8l-88.3-44.2C7.1 311.3.3 300.7 0 288.9s5.9-22.8 16.1-28.7l448-256c10.7-6.1 23.9-5.5 34 1.4z"/></svg>
    </a>
  
</div>
      
    </div>
  </div>
</footer>
      
    </div>
    <div class="md-dialog" data-md-component="dialog">
      <div class="md-dialog__inner md-typeset"></div>
    </div>
    
    
    <script id="__config" type="application/json">{"base": "../..", "features": ["navigation.instant", "navigation.instant.prefetch", "toc.follow", "navigation.top", "search.suggest", "search.highlight", "navigation.footer"], "search": "../../assets/javascripts/workers/search.b8dbb3d2.min.js", "translations": {"clipboard.copied": "\u5df2\u590d\u5236", "clipboard.copy": "\u590d\u5236", "search.result.more.one": "\u5728\u8be5\u9875\u4e0a\u8fd8\u6709 1 \u4e2a\u7b26\u5408\u6761\u4ef6\u7684\u7ed3\u679c", "search.result.more.other": "\u5728\u8be5\u9875\u4e0a\u8fd8\u6709 # \u4e2a\u7b26\u5408\u6761\u4ef6\u7684\u7ed3\u679c", "search.result.none": "\u6ca1\u6709\u627e\u5230\u7b26\u5408\u6761\u4ef6\u7684\u7ed3\u679c", "search.result.one": "\u627e\u5230 1 \u4e2a\u7b26\u5408\u6761\u4ef6\u7684\u7ed3\u679c", "search.result.other": "# \u4e2a\u7b26\u5408\u6761\u4ef6\u7684\u7ed3\u679c", "search.result.placeholder": "\u952e\u5165\u4ee5\u5f00\u59cb\u641c\u7d22", "search.result.term.missing": "\u7f3a\u5c11", "select.version": "\u9009\u62e9\u5f53\u524d\u7248\u672c"}}</script>
    
    
      <script src="../../assets/javascripts/bundle.c8d2eff1.min.js"></script>
      
        <script src="../../javascripts/extra.js"></script>
      
        <script src="../../javascripts/mathjax.js"></script>
      
        <script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
      
        <script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
      
    
  </body>
</html>